function [a,P] = PassBall(P,S,mode,k) % pass the ball to player k

P_X=P{1}(1);
P_Y=P{1}(2);
P_theta=P{1}(3);
P_num = P{3};

B = S{1};
B_X = B{1}(1);
B_Y = B{1}(2);

if(P_num==k)
    a = '';

else
    k = k + 1; % correct k because the ball is in the first index of the state


    Sk=S{k};
    Sk_X=Sk{1}(1);
    Sk_Y=Sk{1}(2);

    dist=sqrt((Sk_X-P_X)^2+(Sk_Y-P_Y)^2); % distance to team mate k
    angle=atan2(Sk_Y-P_Y,Sk_X-P_X)/pi*180 - P_theta;    % compute the angle to teammate k in degrees

    if(angle<0)
        angle=angle+360;    % correct the angle
    end

    angle_to_ball = atan2(B_Y-P_Y,B_X-P_X)/pi*180 - P_theta; % compute the angle to the ball in degrees

    if(angle_to_ball<0)
        angle_to_ball =angle_to_ball+360;    % correct the angle
    end

    if(angle_to_ball > 135 && angle_to_ball < 225)
        a = sprintf('turn %d', angle_to_ball);
    else
        if(dist < 10)
            a = sprintf('kick %d %d', 10, round(angle));
        elseif(dist <20)
            a = sprintf('kick %d %d', 20, round(angle));
        else
            a = sprintf('kick %d %d', 30, round(angle));%round(6+0.33*dist), round(angle));%round(6+0.33*dist), round(angle)); % kick ball to teammate
        end
    end
end;

end